package com.iw.point.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.iw.point.entity.PointRule;
import com.iw.point.service.PointRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alvin.sms.model.SendSmsVO;
import com.iw.adoms.action.BaseAction;
import com.iw.adoms.entity.QrGoodsCode;
import com.iw.adoms.entity.neum.DepartType;
import com.iw.adoms.entity.neum.Status;
import com.iw.adoms.service.QrGoodsCodeService;
import com.iw.core.utils.EncryptUtils;
import com.iw.point.entity.PointMember;
import com.iw.point.entity.PointRecord;
import com.iw.point.entity.PointType;
import com.iw.point.service.PointMemberService;
import com.iw.point.service.PointRecordService;
import com.iw.point.vo.PointChargeVO;
import com.iw.sms.service.AlvinSmsService;

@Controller
@RequestMapping("/point/service")
public class Service400Action extends BaseAction {

    @Autowired
    private PointMemberService pointMemberService;
    @Autowired
    private QrGoodsCodeService qrGoodsCodeService;
    @Autowired
    private PointRecordService pointRecordService;
    @Autowired
    private AlvinSmsService alvinSmsService;
    @Autowired
    private PointRuleService pointRuleService;

    @RequestMapping(value = "/charge")
    public String charge(Model model) {
        return "/point/service/charge";
    }

    @RequestMapping(value = "/chargePoints")
    public String chargePoints(Model model, PointChargeVO vo, HttpServletRequest request) {

        PointMember member = null;
        Integer pointMemberID = vo.getPointMemberID();
        if (pointMemberID == null) {
            member = new PointMember();
            member.setUserName(vo.getsMobile());
            member.setMobile(vo.getsMobile());
            member.setPassword(EncryptUtils.md5(vo.getsMobile()));
            member.setInsertTime(new Date());
            pointMemberService.save(member);
        } else {
            member = pointMemberService.find(pointMemberID);
        }
        Double balance = vo.getcPoint() + member.getBalance();
        member.setBalance(balance); // 给会员加积分
        pointMemberService.update(member);

        Integer qrGoodsCodeID = vo.getQrGoodsCodeID();
        QrGoodsCode qr = qrGoodsCodeService.find(qrGoodsCodeID);
        // 记录积分记录
        PointRecord record = new PointRecord();
        record.setBalance(balance);
        record.setPoint(vo.getcPoint());
        record.setMember(member);
        record.setType(PointType.POINT_TEL);
        record.setInsertTime(new Date());
        record.setiCode(qr.getiCode());
        record.setOper(this.getCurrentUser(request).getName()); //添加操作员
        pointRecordService.save(record);
        qr.setState(Status.QR_HAD_INTEGRAL.getValue());
        qrGoodsCodeService.update(qr);
        String msg = "积分操作成功！";
        if (vo.issNotice()) {
            SendSmsVO svo = new SendSmsVO(vo.getsMobile(), vo.getsContent());
            if (!alvinSmsService.sendSms(svo)) {
                msg = "短信发送失败(服务已关闭)，" + msg;
            }
        }
        model.addAttribute("msg", msg);
        return "/point/service/charge";
    }

    @RequestMapping(value = "/convert")
    public String convert(Model model) {
        return "/point/service/convert";
    }

    @RequestMapping(value = "/convertPoints")
    public String convertPoints(Model model, PointChargeVO vo, HttpServletRequest request) {

        PointMember member = null;
        Integer pointMemberID = vo.getPointMemberID();
        if (pointMemberID == null) {
            member = new PointMember();
            member.setUserName(vo.getsMobile());
            member.setMobile(vo.getsMobile());
            member.setPassword(EncryptUtils.md5(vo.getsMobile()));
            member.setInsertTime(new Date());
            pointMemberService.save(member);
        } else {
            member = pointMemberService.find(pointMemberID);
        }
        Double balance = vo.getcPoint() + member.getBalance();
        member.setBalance(balance); // 给会员加积分
        pointMemberService.update(member);

        Integer ruleId = vo.getRuleId();
        PointRule rule = pointRuleService.find(ruleId);
        // 记录积分记录
        PointRecord record = new PointRecord();
        record.setBalance(balance);
        record.setPoint(vo.getcPoint());
        record.setMember(member);
        record.setType(PointType.CONVERT_TEL);
        record.setIdepart(this.getCurrentDepartment(request));
        record.setInsertTime(new Date());
        record.setiCode(rule.getiCode());
        record.setOper(this.getCurrentUser(request).getName()); //添加操作员
        pointRecordService.save(record);
        String msg = "兑换成功！";
        if (vo.issNotice()) {
            SendSmsVO svo = new SendSmsVO(vo.getsMobile(), vo.getsContent());
            if (!alvinSmsService.sendSms(svo)) {
                msg = "短信发送失败(服务已关闭)，" + msg;
            }
        }
        model.addAttribute("msg", msg);
        return "/point/service/convert";
    }

    @RequestMapping(value = "/search")
    public String search(Model model, String mobile, String iCode) {

        iCode = iCode.trim().replaceAll(" ", "");

        // 2.省代出库后就可以积分（2013.04.21）
//        String hql = "from QrGoodsCode where id in (select g.qrCode.id from Goods g where g.department.id in (select dd.id from Department dd where dd.type.id in "
//                + "(select dt.id from DepartmentType dt where dt.name=?)) and g.state = ? and g.qrCode.id = " + "(select qq.id from QrGoodsCode qq where qq.iCode = ? and qq.state != ?))";
//        List<QrGoodsCode> qrList = qrGoodsCodeService.findList(hql, DepartType.SD.v(), Status.HAS_OUT.getValue(), iCode, Status.QR_HAD_INTEGRAL.getValue());

        //到了省代就可以积分(2013.08.06)
        String hql = "from QrGoodsCode where id in (select g.qrCode.id from Goods g where g.department.id in (select dd.id from Department dd where dd.type.id in "
                + "(select dt.id from DepartmentType dt where dt.name != ?))  and g.qrCode.id = " + "(select qq.id from QrGoodsCode qq where qq.iCode = ? and qq.state != ?))";
        List<QrGoodsCode> qrList = qrGoodsCodeService.findList(hql, DepartType.ZD.v(), iCode, Status.QR_HAD_INTEGRAL.getValue());

        if (qrList != null && !qrList.isEmpty()) {
            model.addAttribute("qrGoods", qrList.get(0));
        }
        PointMember member = pointMemberService.find("mobile", mobile.trim().replaceAll(" ", ""));
        model.addAttribute("member", member);

        return "/point/service/charge";
    }

    // @RequestMapping(value = "/convert")
    // public String convert(Model model) {
    // return "/point/service/convert";
    // }

}
